package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.interest.entity.InterestBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.beiing.tianshuai.tianshuai.widget.RoundAngleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

import static com.beiing.tianshuai.tianshuai.app.HttpRequestConstant.VIDEO_PIC_HEAD;

/**
 * Created by Jinni on 2017/11/20 0020
 */

public class InterestMainAdapter extends BaseRecyclerViewAdapter<InterestBean.DataBean.VideoBean> {

    private Context mContext;

    /**
     * 更多菜单点击事件
     */
    private OnMoreClickListener mOnMoreClickListener;

    /**
     * item点击事件
     */
    private OnItemClickListener mOnItemClickListener;

    /**
     * 头像和昵称的点击事件
     */
    private OnAvatarClickListener mOnAvatarClickListener;

    public InterestMainAdapter(Context context) {
        mContext = context;
    }

    public void setOnMoreClickListener(OnMoreClickListener onMoreClickListener) {
        mOnMoreClickListener = onMoreClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnAvatarClickListener(OnAvatarClickListener onAvatarClickListener) {
        mOnAvatarClickListener = onAvatarClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder result;
        result = new VideoViewHolder(View.inflate(mContext, R.layout.item_interest_video, null));
        return result;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VideoViewHolder) {
            InterestBean.DataBean.VideoBean bean = mList.get(position);
            ((VideoViewHolder) holder).setData(bean);
            ((VideoViewHolder) holder).setListener(bean);
        }
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_video_title)
        TextView mTvVideoTitle;
        @BindView(R.id.rl_user_info_container)
        RelativeLayout mRlUserInfoContainer;
        @BindView(R.id.iv_cover)
        RoundAngleImageView mVideoPlayerStandard;
        @BindView(R.id.btn_more)
        ImageButton mBtnMore;
        @BindView(R.id.tv_video_view_count)
        TextView mViewCount;
        @BindView(R.id.tv_video_tag)
        TextView mVideoTag;
        @BindView(R.id.tv_video_reply_count)
        TextView mVideoReplyCount;
        @BindView(R.id.user_avatar)
        CircleImageView mUserAvatar;
        @BindView(R.id.user_name)
        TextView mUserNickname;

        VideoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(InterestBean.DataBean.VideoBean bean) {
            // 设置回复数量
            mVideoReplyCount.setText(bean.getComment());
            // 标签名称
            mVideoTag.setText(bean.getTypename());
            // 播放次数
            mViewCount.setText(MessageFormat.format("{0}次", bean.getView()));
            // 视频标题
            mTvVideoTitle.setText(bean.getTitle());
            // 视频封面
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(710, 400);
            Glide.with(mContext.getApplicationContext())
                    .load(VIDEO_PIC_HEAD + bean.getCover())
                    .apply(requestOptions)
                    .into(mVideoPlayerStandard);
            // 发表者昵称
            mUserNickname.setText(bean.getName());
            // 发表者头像
            RequestOptions avatarOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(200, 200);
            String photo = bean.getPhoto();
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(photo) ? R.mipmap.default_avatar : HttpRequestConstant.USER_PIC_HEAD + photo)
                    .apply(avatarOptions)
                    .into(mUserAvatar);
        }

        public void setListener(final InterestBean.DataBean.VideoBean bean) {
            mBtnMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnMoreClickListener != null) {
                        mOnMoreClickListener.onMoreClick(bean, getAdapterPosition());
                    }
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(bean, getAdapterPosition());
                    }
                }
            });

            mRlUserInfoContainer.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnAvatarClickListener != null) {
                        mOnAvatarClickListener.onAvatarClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnMoreClickListener {
        void onMoreClick(InterestBean.DataBean.VideoBean bean, int position);
    }

    public interface OnItemClickListener {
        void onItemClick(InterestBean.DataBean.VideoBean bean, int position);
    }

    public interface OnAvatarClickListener {
        void onAvatarClick(InterestBean.DataBean.VideoBean bean, int position);
    }
}
